//
//  ShopClassViewController.m
//  WZCLApp
//
//  Created by chirui on 2018/7/6.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "ShopClassViewController.h"
#import "ClassListCell.h"
#import "ComBoListTableViewCell.h"
#import "ShopMainViewController.h"
#import "ShopOrderListViewController.h"
#import "AddressListViewController.h"


@interface ShopClassViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
     NSInteger buttonCurryIndex;
    NSArray * classArray;
    NSMutableArray * _dataArray;
    UITableView * _classTableView; // 分类列表
    UITableView * _comboTableView; // 列表
    int i;
    int _page;
    int _totailPage;
    NSString * cateidStr;
    UIView * noBodyView;
}

@end

@implementation ShopClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    i = 0;
    TitleView(@"分类");
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_return_back" target:self action:@selector(backClick)];
    _page = 1;
    
     noBodyView = [NothingMessageView creatNothingMessageViewFrame:CGRectMake(100+(WIDTH-300)/2, (HEIGHT-64)/2-100, 200, 200) andMessageStr:@"还没有相关内容~" onView:nil];
    _dataArray = @[].mutableCopy;
    [self creatTableView];
    [self creatComboTableView];
    [self creatTabbarImageView];
    [self getClassListRequest];
}

-(void)backClick{
    
     [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)creatTabbarImageView{
    
    NSArray * imageArray = @[@"icon_tab_hot_pre",@"icon_tab_classification",@"icon_tab_order_pre",@"icon_tab_add-pre"];
    NSArray * titleArray = @[@"热卖",@"分类",@"订单",@"收货地址"];
    UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0,HEIGHT-49-SH_TopHeight, WIDTH, 49)];
    view.backgroundColor =[UIColor whiteColor];
    UIView * lineView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 0.5)];
    lineView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    [view addSubview:lineView];
    [self.view addSubview:view];
    for (int j=0; j<4;j++) {
        UIButton * endThreeBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        endThreeBtn.frame = CGRectMake((WIDTH/4)*j, 5, WIDTH/4,32);
        [endThreeBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArray[j]]] forState:UIControlStateNormal];
        [endThreeBtn addTarget:self action:@selector(downBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        endThreeBtn.tag =1000+j;
        
        [view addSubview:endThreeBtn];
        
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake((WIDTH/4)*j,36, WIDTH/4,12)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = titleArray[j];
        label.font = [UIFont systemFontOfSize:10];
        label.textColor = [UIColor lightGrayColor];
        [view addSubview:label];
        if (j==1) {
            label.textColor = [UIColor orangeColor];
        }
    }
    [self.view addSubview:view];
    
}

-(void)downBtnClick:(UIButton *)btn {
    
    if (btn.tag == 1000) {
        
        ShopMainViewController * shopClass =[[ShopMainViewController alloc]init];
        [self.navigationController pushViewController:shopClass animated:NO];
    }
    if (btn.tag == 1002) {
        
        ShopOrderListViewController * shoper =[[ShopOrderListViewController alloc]init];
        [self.navigationController pushViewController:shoper animated:NO];
    }
    if (btn.tag == 1003) {
        
        AddressListViewController  * address =[[AddressListViewController alloc]init];
        [self.navigationController pushViewController:address animated:NO];
    }
    
}

-(void)creatTableView{
    _classTableView  =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 100, HEIGHT- SH_TabBarHeight) style:UITableViewStyleGrouped];
    _classTableView.delegate =self;
    _classTableView.dataSource =self;
    _classTableView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    _classTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_classTableView];
}


-(void)creatComboTableView {
    
    _comboTableView =[[UITableView alloc]initWithFrame:CGRectMake(100, 0, WIDTH - 100, HEIGHT -SH_TabBarHeight) style:UITableViewStyleGrouped];
    _comboTableView.delegate =self;
    _comboTableView.dataSource =self;
    _comboTableView.backgroundColor =[UIColor whiteColor];
    UIView * view =[[UIView alloc]init];
    _comboTableView.tableFooterView =view;
    //    _comboTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_comboTableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    if (tableView ==_classTableView) {
        
        
        return classArray.count;
    }else {
        
        return _dataArray.count;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
   
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
        return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == _classTableView) {
        ClassListCell *   cell = [[[NSBundle mainBundle] loadNibNamed:[ClassListCell reuseIdentifier] owner:nil options:nil] lastObject];
        NSDictionary * dic = classArray[indexPath.row];
        cell.titleLabel.text = dic[@"cate_name"];
        
        if (indexPath.row==i) {
            cell.titleLabel.backgroundColor =[UIColor whiteColor];
            cell.lineView.hidden =NO;
            cell.titleLabel.textColor = [UIColor blackColor];
        }else {
           
            cell.lineView.hidden =YES;
            
        }
        return cell;
    }else{
        
        NSDictionary * dic = _dataArray[indexPath.row];
        
        ComBoListTableViewCell *   cell = [[[NSBundle mainBundle] loadNibNamed:[ComBoListTableViewCell reuseIdentifier] owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        [cell.shopImage sd_setImageWithURL:ImageUrl(dic[@"cover_photo"]) placeholderImage:ImageName(@"img_new_default")];
    
        cell.foodName.text = dic[@"product_name"];
        
      
        cell.priceLabel.text = [NSString stringWithFormat:@"￥%@",dic[@"sale_price"]];
  
 
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _classTableView) {
        return 50;
    }else {
        return 110;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView == _classTableView) {
        i = (int)indexPath.row;
        [_classTableView reloadData];
        NSDictionary * dic = classArray[indexPath.row];
        ClassListCell *   cell_choose = [tableView cellForRowAtIndexPath:indexPath];
        _page = 1;
        cateidStr = dic[@"cate_id"];
        cell_choose.lineView.hidden =NO;
        cell_choose.titleLabel.backgroundColor =[UIColor whiteColor];
        cell_choose.titleLabel.textColor = [UIColor blackColor];
        cell_choose.bgView.backgroundColor =[UIColor whiteColor];
        [self getClassListRequest];
        
    }else if (tableView == _comboTableView){
        
//        NSDictionary * dic = dataArray[indexPath.row];{
//
//            FoodsDetailsWebViewController * details  =[FoodsDetailsWebViewController new];
//            details.url = dic[@"web_view_url"];
//            [self.navigationController pushViewController:details animated:YES];
//            NSLog(@"详情");
//        }
    }
}

-(void)getClassListRequest{
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(class_titleClass_api) andparameters:nil Success:^(id responseObject) {
        SHuiMBHidleYes;
        
        if (codeSucess) {
            
            if ([responseObject[@"datas"] isKindOfClass:[NSArray class]]) {
                 classArray = responseObject[@"datas"];
            }
           
            cateidStr = classArray[i][@"cate_id"];
            [self getClassDataRequest];
            [_classTableView reloadData];
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
    }];
    
}

-(void)getClassDataRequest{
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_comboTableView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{@"page":[NSString stringWithFormat:@"%d",_page],
                           @"cate_id":cateidStr,
                           @"per_page":@"10"
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_classGoods_api) andparameters:dic Success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue]==200) {
            
            _totailPage = [responseObject[@"datas"][@"total_page"]  intValue];
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
                
            }else {
                
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            if (_dataArray.count>0) {
                noBodyView.hidden =YES;
                if (_dataArray.count<10) {
                    
                _comboTableView.mj_footer.hidden = YES;
                }
            }else {
                _comboTableView.mj_footer.hidden = YES;
                noBodyView.hidden =NO;
                
                [self.view addSubview:noBodyView];
            }
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
            
        }
        
        [_comboTableView.mj_footer endRefreshing];
        [_comboTableView.mj_header  endRefreshing];
        [_comboTableView reloadData];
        
        
    } failed:^(NSError *error) {
        [_comboTableView.mj_header endRefreshing];
        [_comboTableView reloadData];
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
    }];
    
    
}

@end
